/**
 * Title:       MessageTransportService.java
 * Copyright:   Copyright (c) 1996-2004 The Agent Factory Working Group. All rights reserved.
 * Licence:     This file is free software; you can redistribute it and/or modify
 *              it under the terms of the GNU Lesser General Public License as published by
 *              the Free Software Foundation; either version 2.1, or (at your option)
 *              any later version.
 *
 *              This file is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU Lesser General Public License for more details.
 *
 *              You should have received a copy of the GNU Lesser General Public License
 *              along with Agent Factory; see the file COPYING.  If not, write to
 *              the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 *              Boston, MA 02111-1307, USA.
 */
package com.agentfactory.platform.mts;

import java.util.Hashtable;

import com.agentfactory.platform.core.AgentID;
import com.agentfactory.platform.core.Agent;
import com.agentfactory.platform.service.PlatformService;
import com.agentfactory.platform.service.PlatformServiceDescriptor;
import com.agentfactory.platform.service.PlatformServiceManager;
/**
 * This interface outlines the methods that should be implemented by any
 * message transport service.
 *
 * @author  Rem Collier
 */
public abstract class MessageTransportService extends PlatformService {
    protected Hashtable agents;
    protected PlatformServiceManager manager;
    
    public void init(PlatformServiceDescriptor descriptor, PlatformServiceManager manager) {
        super.init(descriptor, manager);
        this.manager = manager;
        agents = new Hashtable();
    }
    
    public abstract boolean isValidService(String address);
    public abstract boolean sendMessage(String address, Message message);
    public abstract String getAddress();
    public abstract String getServiceID();
    
    public void bind(Agent agent) {
        agents.put(agent.getName(), agent);
        addObserver(agent);
    }
    
    public void unbind(Agent agent) {
        agents.remove(agent.getName());
    }
    
    public void modifyBinding(String oldName, String name) {
        Object object = agents.get(oldName);
        agents.put(name, object);
        agents.remove(oldName);
    }
    
    public boolean receiveMessage(Message message) {
        boolean success = true;
        
        AgentID aid = (AgentID) message.getReceivers().get(0);
        Agent agent = null;
        
        int index = aid.getName().indexOf('*');
        if (index == -1) {
            agent = (Agent) agents.get(aid.getName());
            if (agent != null) {
                agent.getMessageQueue().receiveMessage(message);
            } else {
                success = false;
            }
        } else {
            String pattern = aid.getName().substring(0, index);
            
            String name = null;
            synchronized (agents) {
                java.util.Iterator it = agents.keySet().iterator();
                while (it.hasNext()) {
                    name = (String) it.next();
                    if (name.startsWith(pattern)) {
                        agent = (Agent) agents.get(name);
                        agent.getMessageQueue().receiveMessage(message);
                    }
                }
            }
            
        }
        
        return success;
    }
    
    public void start() {
    }
}
